import os
from PIL import Image, ImageDraw, ImageFont

# 指定目录路径
directory = r'.\results\res\xsum'

png_names = ["gpt2-xl/gpt2-xl", "gpt2-xl/gpt-neo-2.7B", "gpt2-xl/opt-2.7B",
             "gpt-neo-2.7B/gpt2-xl", "gpt-neo-2.7B/gpt-neo-2.7B", "gpt-neo-2.7B/opt-2.7B",
             "opt-2.7B/gpt2-xl", "opt-2.7B/gpt-neo-2.7B", "opt-2.7B/opt-2.7B"]

# 获取目录中的前九个文件
files = sorted(os.listdir(directory))[:9]

# 打开九张图片
images = [Image.open(os.path.join(directory, file, 'hist.png')) for file in files]

# 假设所有图片的大小相同，获取单张图片的宽和高
width, height = images[0].size

# 创建一个新的空白图片，大小为3倍宽和3倍高
result = Image.new('RGB', (width * 3, height * 3 + 50), (255, 255, 255))

# 创建一个绘图对象
draw = ImageDraw.Draw(result)

# 使用默认字体
font_size = 25
font = ImageFont.load_default(font_size)

# 将九张图片按顺序粘贴到新图片上
for i, image in enumerate(images):
    x = (i % 3) * width
    y = (i // 3) * height + 30
    result.paste(image, (x, y))
    
# 添加标签
for i, image in enumerate(images):
    x = (i % 3) * width
    y = (i // 3) * height + 30
    
    # 添加上面的标签
    label_x = x + width // 2
    label_y = y + height
    draw.text((label_x, label_y), png_names[i], fill="black", font=font, anchor="mm")

# 保存结果图片
result.save('result.png')